/**
 * Created by wangjiakun on 2018/6/14 0014.
 */
import React from 'react';
import {messages} from 'share/common';
import {Form, Row, Col, Table, Switch, Checkbox, Modal, Button, Input, Select, Divider, Spin, message, Tooltip, Icon} from 'antd';

export default {

  changeByCode (code,status){
    switch (code){
      case 'EXPENSE_LEVEL': return messages('setting.key1153'/*补贴费用类型*/);
        break;
      case 'USER_GROUPS': return messages('setting.key1154'/*适用人员*/);
        break;
      case 'ALLOWANCE_CITY': return messages('setting.key1155'/*差补城市*/);
        break;
      case 'CURRENCY_CODE': return messages('setting.key1156'/*币种*/);
        break;
      case 'ALLOWANCE_AMOUNT': return messages('setting.key1157'/*金额*/);
        break;
      case 'TRAVEL_HOURS': return messages('setting.key1158'/*出差时长*/);
        break;
      case 'TRAVEL_DAYS': return messages('setting.key1159'/*出差天数*/);
        break;
      case 'HOLIDAY_WEEKEND': return messages('setting.key1160'/*周末*/);
        break;
      case 'DEPARTURE_DAY':
        // return status === 'edit' ? '出差当天：根据差旅申请单开始日期和时间，可单独设置该天补贴标准' : '出差当天';
        return status === 'edit' ? `${messages('setting.key1161'/*出差当天*/)}：${messages('setting.key1162'/*根据差旅申请单开始日期和时间，可单独设置该天补贴标准*/)}` : messages('setting.key1161'/*出差当天*/);
        break;
      case 'RETURN_DAY':
        // return status === 'edit' ? '返回当天：根据差旅申请单结束日期和时间，可单独设置该天补贴标准' : '返回当天';
        return status === 'edit' ? `${messages('setting.key1163'/*返回当天*/)}：${messages('setting.key1164'/*根据差旅申请单结束日期和时间，可单独设置该天补贴标准*/)}` : messages('setting.key1163'/*返回当天*/);
        break;
      default:break;
    }
  },

  typeBaseHeadLayout (typeBaseData){
    if(!typeBaseData){
      return (<div></div>) ;
    }
    let base = typeBaseData.dimensionTypes[0];
    let travel = typeBaseData.dimensionTypes[1];
    let special = typeBaseData.dimensionTypes[2];
    let itinerary = typeBaseData.dimensionTypes[3];
    let baseStr = "";
    let travelStr = "";
    let specialStr = "";
    let itineraryStr = "";
    let tipStr = typeBaseData.withTips ? typeBaseData.tips : messages('setting.key1165'/*不提示*/);
    let selectAllStr = typeBaseData.subsidySelection ? messages('common.yes'/*是*/) : messages('common.no'/*否*/);
    let autoSubsidyStr = typeBaseData.applicationAutoSubsidy ? messages('common.yes'/*是*/) : messages('common.no'/*否*/);
    let isHave = false;
    base.dimensionDetails.map(item=>{
      if(item.selected){
        isHave = true;
        baseStr = baseStr + this.changeByCode(item.code) + ' , '
      }
    });
    if(isHave){
      baseStr = baseStr.substr(0,baseStr.length - 2);
    }else{
      baseStr = '——';
    }
    isHave = false;
    travel.dimensionDetails.map(item=>{
      if(item.selected){
        isHave = true;
        travelStr = travelStr + this.changeByCode(item.code) + ' , '
      }
    });
    if(travel.formFieldDetails){
      travel.formFieldDetails.map(item=>{
        if(item.selected){
          isHave = true;
          travelStr = travelStr + item.name + ' , '
        }
      });
    }
    if(isHave){
      travelStr = travelStr.substr(0,travelStr.length - 2);
    }else{
      travelStr = '——'
    }
    isHave = false;
    special.dimensionDetails.map(item=>{
      if(item.selected){
        isHave = true;
        specialStr = specialStr + this.changeByCode(item.code) + ' , '
      }
    });
    if(isHave){
      specialStr = specialStr.substr(0,specialStr.length - 2);
    }else{
      specialStr = '——'
    }
    isHave = false;
    itinerary.itineraryFieldList && itinerary.itineraryFieldList.map(item => {
      isHave = true;
      itineraryStr += item.name + ' , ';
    });
    if(isHave){
      itineraryStr = itineraryStr.substr(0, itineraryStr.length - 2);
    }else{
      itineraryStr = '——';
    }
    const layout = {
      title:6,
      content:18
    }
    return(
      <div>
        <Row>
          <Col span={layout.title} style={{maxWidth:160}}>
            <h3>{messages('setting.key1104'/*默认规则*/)}</h3>
          </Col>
          <Col span={layout.content}>
            {baseStr}
          </Col>
        </Row>
        <Row>
          <Col span={layout.title} style={{maxWidth:160}}>
            <h3>{messages('setting.key1105'/*申请单字段*/)}</h3>
          </Col>
          <Col span={layout.content}>
            {travelStr}
          </Col>
        </Row>
        <Row>
          <Col span={layout.title} style={{maxWidth:160}}>
            <h3>{messages('expense-reimbursement-control.key87')/*行程字段*/}</h3>
          </Col>
          <Col span={layout.content}>
            {itineraryStr}
          </Col>
        </Row>
        <Row>
          <Col span={layout.title} style={{maxWidth:160}}>
            <h3>{messages('setting.key1106'/*特殊控制*/)}</h3>
          </Col>
          <Col span={layout.content}>
            {specialStr}
          </Col>
        </Row>
        <Row>
          <Col span={layout.title} style={{maxWidth:160}}>
            <h3>{messages('setting.key1107'/*提示内容*/)}</h3>
          </Col>
          <Col span={layout.content}>
            {tipStr}
          </Col>
        </Row>
        <Row>
          <Col span={layout.title} style={{maxWidth:160}}>
            <h3>{messages('expense-reimbursement-control.key1')/*差补类型默认全选*/}</h3>
          </Col>
          <Col span={layout.content}>
            {selectAllStr}
          </Col>
        </Row>
        <Row>
          <Col span={layout.title} style={{maxWidth:160}}>
            <h3>
              {messages('expense-reimbursement-control.key96')/*申请单自动差补*/}&nbsp;
              <Tooltip
                title={messages('expense-reimbursement-control.key97')/*开启申请单自动差补功能，将在填写申请单时，自动根据参与人、往返日期计算和保存差补行程；不可与默认规则-差补城市同时启用。*/}
                placement="bottom"
              >
                <Icon type="info-circle-o" />
              </Tooltip>
            </h3>
          </Col>
          <Col span={layout.content}>
            {autoSubsidyStr}
          </Col>
        </Row>
      </div>
    )
  },

  //校验行程值列表是否有变动，以str形式返回变动项
  judgeIsChangeCust(oldCustList, newCustList) {
    let str = '';
    oldCustList && oldCustList.map(oldCust => {
      let isExist = false;
      newCustList && newCustList.map(newCust => {
        if (oldCust.customEnumerationOID === newCust.customEnumerationOID) {
          isExist = true;
        }
      });
      if (!isExist) {
        str += oldCust.name + ' , ';
      }
    });
    newCustList && newCustList.map(newCust => {
      let isExist = false;
      oldCustList && oldCustList.map(oldCust => {
        if (oldCust.customEnumerationOID === newCust.customEnumerationOID) {
          isExist = true;
        }
      });
      if (!isExist) {
        str += newCust.name + ' , ';
      }
    });
    return str;
  },

  restrictionChange(type){
    switch (type){
      case '+':return 1003;break;
      case '-':return 1004;break;
      case '*':return 1001;break;
      case '/':return 1002;break;
      case 1003 :return '+';break;
      case 1004 :return '-';break;
      case 1001 :return '*';break;
      case 1002 :return '/';break;
    }
  },

  formatNumber (num){
    return num;
  },

  calculateWeek(type,standard,base){
    if(!standard){
      standard = 0;
    }
    if(!base){
      base = 0;
    }
    switch (type){
      case '+':return this.formatNumber(standard + base);break;
      case '-':
        if((standard - base)< 0){
          message.error(messages('setting.key1166'/*结果不能小于0*/));
        }
        return this.formatNumber(standard - base);
        break;
      case '*':return this.formatNumber(standard * base);break;
      case '/':
        if(base !==0 ){
          return this.formatNumber(standard / base);
        }else{
          message.error(messages('setting.key1167'/*除数不能为0*/));
        }
        break;
    }
  },

  //自定义规则 范围属性
  fieldRange(){
    let range = [
      {name:messages('setting.key1133'/*包含*/),value:1006},
      {name:messages('setting.key1134'/*不包含*/),value:1007},
      {name:messages('setting.key1135'/*为空*/),value:1009},
      {name:messages('setting.key1136'/*不为空*/),value:1010},
    ]
    return range;
  },

  //解析区间
  parseRange(value){
    let arr = [];
    arr.push(value.substr(0,1));
    arr.push(value.substring(1,value.indexOf(',')));
    arr.push(value.substring(value.indexOf(',') + 1,value.length - 1));
    arr.push(value.substr(value.length - 1,1));
    let result = [];
    arr.map(char =>{
      switch (char){
        case '(':result.push(1002);break;
        case '[':result.push(1003);break;
        case ']':result.push(1005);break;
        case ')':result.push(1004);break;
        default:result.push(JSON.parse(char));
      }
    });
    return result;
  },

  /**
   * 解析规则详情
   * @param detailItem 规则详情
   * @param type 类型
   * @param witch 第几个 如果type为CUST_LIST, ITINERARY_CUST_LIST  witch的值为customEnumerationOID
   * @param defalut 默认值
   * @param num 自定义规则的地x个值
   * @returns {string}
   */
  parseDetailRule(detailItem, type, witch, defalut, num, readonly = false){
    let travelCondition = detailItem.travelSubsidiesConditions ? detailItem.travelSubsidiesConditions : [];
    let result = '';//结果
    switch (type){
      case 'TRAVEL_DAYS':
        travelCondition.map(item =>{
          if(item.messageKey === 'TRAVEL_DAYS'){
            if(item.restriction !== 1008){//非区间值
              if(item.restriction !== 1000){//非空值
                if(item.restriction === 1002 || item.restriction === 1003){//大于
                  if(witch === 1){
                    result = item.values ? JSON.parse(item.values) : '';
                  }else if(witch === 2){
                    result = defalut;
                  } else{
                    result = witch === 0 ? item.restriction : defalut;
                  }
                }else{
                  if(witch === 2){
                    result = item.values ? JSON.parse(item.values) : '';
                  }else if(witch === 1){
                    result = defalut;
                  }else{
                    result = witch === 3 ? item.restriction : defalut;
                  }
                }
              }else{
                result = defalut;
              }
            }else{//区间值走这里解析
              result = this.parseRange(item.values)[witch];
            }
          }});
        break;
      case 'TRAVEL_HOURS':
        travelCondition.map(item =>{
          if(item.messageKey === 'TRAVEL_HOURS'){
            if(item.restriction !== 1008){//非区间值
              if(item.restriction !== 1000){//非空值
                if(item.restriction === 1002 || item.restriction === 1003){//大于
                  if(witch === 1){
                    result = item.values ? JSON.parse(item.values) : '';
                  }else if(witch === 2){
                    result = defalut;
                  } else{
                    result = witch === 0 ? item.restriction : defalut;
                  }
                }else{
                  if(witch === 2){
                    result = item.values ? JSON.parse(item.values) : '';
                  }else if(witch === 1){
                    result = defalut;
                  }else{
                    result = witch === 3 ? item.restriction : defalut;
                  }
                }
              }else{
                result = defalut;
              }
            }else{//区间值走这里解析
              result = this.parseRange(item.values)[witch];
            }
          }
        });
        break;
      case 'USER_GROUPS':
        travelCondition.map(item =>{
          if(item.messageKey === 'USER_GROUPS' && num === 0) {
            result = item.restriction !== 1000 ? item.restriction : 1006;//1000为老数据的通用，'通用'为新数据的'包含通用'1006
          }
          if(item.messageKey === 'USER_GROUPS' && detailItem.userGroups && num === 1){
            if(item.values){
              result = [];
              detailItem.userGroups.length && detailItem.userGroups.map(userGroup => {
                let user = {
                  id: userGroup.id,
                  name: userGroup.name,
                  userGroupOID: userGroup.userGroupOID
                };
                result.push(user);
              });
            }
          }
        });
        if(!travelCondition.length && num === 0) {
          result = 1006;//默认为包含
        }
        break;
      case 'ALLOWANCE_CITY':
        travelCondition.map(item =>{
          if(item.messageKey === 'ALLOWANCE_CITY' && num === 0) {
            result = item.restriction !== 1000 ? item.restriction : 1006;//1000为老数据的通用，'通用'为新数据的'包含通用'1006
          }
          if(item.messageKey === 'ALLOWANCE_CITY' && num === 1){
            // result = item.values ? item.values.split(',') : [];
            result =  readonly ? detailItem.cityGroupNames&&detailItem.cityGroupNames.split(','):(item.values ? item.values.split(',') : []);
          }
        });
        if(!travelCondition.length && num === 0) {
          result = 1006;//默认为包含
        }
        break;
      case 'CUST_LIST':
        let cust = [];
        (travelCondition || []).map(item => {
          if (item.messageKey === 'CUST_LIST') {
            cust.push(item);
          }
        });
        if(cust.length > 0){
          cust.map(item => {
            if (item.dataSource === witch && num === 0) {
              result = item.restriction;
            }
            if (item.dataSource === witch && num === 1) {
              result = item.values ? item.values.split(',') : [];
            }
          });
        }
        break;
      case 'ITINERARY_CUST_LIST':
        let itinerary_cust = [];
        (travelCondition || []).map(item => {
          if (item.messageKey === 'ITINERARY_CUST_LIST') {
            itinerary_cust.push(item);
          }
        });
        if(itinerary_cust.length > 0){
          itinerary_cust.map(item => {
            if (item.fieldCode === witch && num === 0) {
              result = item.restriction;
            }
            if (item.fieldCode === witch && num === 1) {
              result = item.values ? item.values.split(',') : [];
            }
          });
        }
        break;
    }
    return (result === 0 || result) ? result : defalut;
  }

}
